//
//  LGMainInspectNormalEditCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/1.
//

#import "LGMainInspectNormalEditCell.h"

#define nameWidth     (isPad ? viewPix(200) : viewPix(95))
#define resultWidth   (isPad ? viewPix(120) : viewPix(60))
#define unitWidth     (isPad ? viewPix(120) : viewPix(60))
#define rangeWidth    (Screen_W-viewPix(32)-nameWidth-resultWidth-unitWidth-viewPix(20)-viewPix(15))

@interface LGMainInspectNormalEditCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UITextField *resultTF;
/**   */
@property (nonatomic , strong)UIView *rangeLineView;
/**   */
@property (nonatomic , strong)UITextField *rangeMinTF;
/**   */
@property (nonatomic , strong)UITextField *rangeMaxTF;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;
/**   */
@property (nonatomic , strong)CAGradientLayer *shadowLayer;

@end

@implementation LGMainInspectNormalEditCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *result = LGNSString(dataDic[@"result"]);
    self.resultTF.text = [result isEqualToString:@"-"]?@"":result;
    self.nameLabel.text = LGNSString(dataDic[@"name"]);
    self.unitLabel.text = LGNSString(dataDic[@"unit"]);
    
    //参考值
    NSArray *rangeArray = dataDic[@"range_value"];
    if(rangeArray.count>0){
        self.rangeMinTF.text = LGNSString(rangeArray[0]);
        self.rangeMaxTF.text = (rangeArray.count>1)?LGNSString(rangeArray[1]):@"";
    }else{
        self.rangeMinTF.text = @"";
        self.rangeMaxTF.text = @"";
    }
    
    //计算最大高度
    CGFloat nameHeight = [self.nameLabel.text textHeightWithFont:LGFont(13) maxWidth:nameWidth];
    CGFloat resultHeight = [result textHeightWithFont:LGFontWeight(13, UIFontWeightMedium) maxWidth:resultWidth];
    CGFloat unitHeight = [self.unitLabel.text textHeightWithFont:LGFont(13) maxWidth:unitWidth];
    CGFloat tempHeight = nameHeight;
    tempHeight = (resultHeight>tempHeight)?resultHeight:tempHeight;
    tempHeight = (unitHeight>tempHeight)?unitHeight:tempHeight;
    tempHeight = (tempHeight<viewPix(28))?viewPix(28):tempHeight;
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(viewPix(44)+tempHeight+3));
    }];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.dataDic];
    //结果
    NSString *content = (self.resultTF.text.length>0)?self.resultTF.text:@"-";
    [tempDic setValue:content forKey:@"result"];
    
    //最大值、最小值
    NSString *minString = (self.rangeMinTF.text.length>0)?self.rangeMinTF.text:@"";
    NSString *maxString = (self.rangeMaxTF.text.length>0)?self.rangeMaxTF.text:@"";
    [tempDic setValue:@[minString,maxString] forKey:@"range_value"];
    NSString *range = @"-";
    if(minString.length>0){
        range = [NSString stringWithFormat:@"%@-",minString];
    }
    if(maxString.length>0){
        range = [NSString stringWithFormat:@"%@%@",range,maxString];
    }
    [tempDic setValue:range forKey:@"range"];
    if(self.delegate  && [self.delegate respondsToSelector:@selector(mainInspectEditChanged: index:key:)]){
        [self.delegate mainInspectEditChanged:tempDic index:self.index key:self.key];
    }
}

-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.resultTF];
    [self.contentView addSubview:self.rangeLineView];
    [self.contentView addSubview:self.rangeMinTF];
    [self.contentView addSubview:self.rangeMaxTF];
    [self.contentView addSubview:self.unitLabel];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.contentView).offset(viewPix(16));
        make.top.right.equalTo(self.contentView).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(44)+16));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.top.equalTo(self.contentView).offset(viewPix(10));
        make.width.equalTo(@(nameWidth));
    }];
    
    [self.resultTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.nameLabel.mas_right).offset(viewPix(5)+resultWidth/2.0);
        make.centerY.equalTo(self.nameLabel);
        make.width.equalTo(@(viewPix(46)));
        make.height.equalTo(@(viewPix(28)));
    }];
    
    [self.rangeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.nameLabel.mas_right).offset(viewPix(10)+resultWidth+rangeWidth/2.0);
        make.centerY.equalTo(self.nameLabel);
        make.width.equalTo(@(viewPix(8)));
        make.height.equalTo(@(1));
    }];
    
    [self.rangeMinTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rangeLineView.mas_left).offset(-viewPix(1));
        make.centerY.equalTo(self.rangeLineView);
        make.width.equalTo(@(viewPix(40)));
        make.height.equalTo(@(viewPix(28)));
    }];
    
    [self.rangeMaxTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rangeLineView.mas_right).offset(viewPix(1));
        make.centerY.width.height.equalTo(self.rangeMinTF);
    }];
    
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(5));
        make.centerY.equalTo(self.nameLabel);
        make.width.equalTo(@(unitWidth));
    }];
}

-(CAGradientLayer *)shadowLayer{
    if(!_shadowLayer){
        _shadowLayer = [[CAGradientLayer alloc]init];
        _shadowLayer.frame = self.bounds;
        _shadowLayer.startPoint = CGPointMake(0.5, 0);
        _shadowLayer.endPoint = CGPointMake(0.5, 1);
        _shadowLayer.colors = @[(__bridge id)[UIColor colorWithString:@"#FFFFFF" alpha:1].CGColor, (__bridge id)[UIColor colorWithString:@"#FFFFFF" alpha:1].CGColor];
        _shadowLayer.locations = @[@(0), @(1.0f)];
    }
    return _shadowLayer;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _nameLabel;
}

-(UITextField *)resultTF{
    if(!_resultTF){
        _resultTF = [[UITextField alloc]init];
        _resultTF.tintColor = [UIColor colorWithString:@"#333333"];
        _resultTF.textColor = [UIColor colorWithString:@"#333333"];
        _resultTF.textAlignment = NSTextAlignmentCenter;
        _resultTF.keyboardType = UIKeyboardTypeDecimalPad;
        _resultTF.font = LGFontWeight(13, UIFontWeightMedium);
        _resultTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _resultTF.layer.borderWidth = 1;
        _resultTF.cornerRidus = 5;
        [_resultTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _resultTF;
}

-(UIView *)rangeLineView{
    if(!_rangeLineView){
        _rangeLineView = [[UIView alloc] init];
        _rangeLineView.backgroundColor = [UIColor colorWithString:@"#666666"];
    }
    return _rangeLineView;
}

-(UITextField *)rangeMinTF{
    if(!_rangeMinTF){
        _rangeMinTF = [[UITextField alloc]init];
        _rangeMinTF.tintColor = [UIColor colorWithString:@"#333333"];
        _rangeMinTF.textColor = [UIColor colorWithString:@"#333333"];
        _rangeMinTF.textAlignment = NSTextAlignmentCenter;
        _rangeMinTF.keyboardType = UIKeyboardTypeDecimalPad;
        _rangeMinTF.font = LGFontWeight(13, UIFontWeightMedium);
        _rangeMinTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _rangeMinTF.layer.borderWidth = 1;
        _rangeMinTF.cornerRidus = 5;
        [_rangeMinTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _rangeMinTF;
}

-(UITextField *)rangeMaxTF{
    if(!_rangeMaxTF){
        _rangeMaxTF = [[UITextField alloc]init];
        _rangeMaxTF.tintColor = [UIColor colorWithString:@"#333333"];
        _rangeMaxTF.textColor = [UIColor colorWithString:@"#333333"];
        _rangeMaxTF.textAlignment = NSTextAlignmentCenter;
        _rangeMaxTF.keyboardType = UIKeyboardTypeDecimalPad;
        _rangeMaxTF.font = LGFontWeight(13, UIFontWeightMedium);
        _rangeMaxTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _rangeMaxTF.layer.borderWidth = 1;
        _rangeMaxTF.cornerRidus = 5;
        [_rangeMaxTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _rangeMaxTF;
}

-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"单位" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _unitLabel;
}


@end
